/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helptest;

import Globals.AllGlobal;
import brain.MyBrain;
import controller.MyController;
import database.operate.OperateMyDatabaseImpl;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Pagination;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;
import javax.swing.JFrame;
import model.DiCeng_Model;
import myfx.component.MyCard;

/**
 *
 * @author Administrator
 */
public class DiCengOper extends JFrame{
    private Pagination pagination;
    private Group root=new Group();
    private Group pageGroup=new Group();
    private Button sjtButton=new Button("生成设计图");
    private Button tytsButton=new Button("生成上表面投影图");
    private Button tytxButton=new Button("生成下表面投影图");
    private Button jietButton=new Button("生成截图并保存");
    private MyController controller;
    private MyBrain brain;
    private Scene scene;
    private JFXPanel fxContainer;
    private ArrayList<DiCeng_Model> dclist=new ArrayList<DiCeng_Model>();
    public DiCengOper(Stage owner, boolean modality, String title,final MyController controller){
        fxContainer=new JFXPanel();
        fxContainer.setPreferredSize(new Dimension(170,480));
        add(fxContainer, BorderLayout.CENTER);
        setSize(170, 480);
        setResizable(false);
        Platform.runLater(new Runnable() {
            
            @Override
            public void run() {
                createScene();		
            }
        });
        
        this.controller=controller;
        
    }
    public void createScene(){
        scene=new Scene(root, 170, 480, Color.WHITE);
        //背景
        MyCard mc=new MyCard(150, 45, "设计施工图", Color.LIGHTGRAY);
        mc.setTranslateY(5);
        mc.setTranslateX(5);
        root.getChildren().add(mc);
        MyCard mc1=new MyCard(150, 260, "生成投影图", Color.LIGHTGRAY);
        mc1.setTranslateY(80);
        mc1.setTranslateX(5);
        root.getChildren().add(mc1);
        MyCard mc2=new MyCard(150, 45, "生成为图片", Color.LIGHTGRAY);
        mc2.setTranslateY(370);
        mc2.setTranslateX(5);
        root.getChildren().add(mc2);
        //button
        sjtButton.setPrefSize(140, 30);
        sjtButton.setTranslateX(10);
        sjtButton.setTranslateY(35);
        root.getChildren().add(sjtButton);
        tytsButton.setPrefSize(140, 30);
        tytsButton.setTranslateX(10);
        tytsButton.setTranslateY(110);
        tytxButton.setPrefSize(140, 30);
        tytxButton.setTranslateX(10);
        tytxButton.setTranslateY(145);
        jietButton.setPrefSize(140, 30);
        jietButton.setTranslateX(10);
        jietButton.setTranslateY(400);
        root.getChildren().add(jietButton);
        pagination = new Pagination(7, 0);
        pagination.setMaxPageIndicatorCount(5);
        pagination.setPrefSize(150, 250);
        pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);
        pagination.setTranslateY(100);
        pagination.setTranslateX(5);
        
        pagination.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer pageIndex) {          
                return createPage(pageIndex);               
            }
        });
        root.getChildren().addAll(pagination);
        root.getChildren().add(tytsButton);
        root.getChildren().add(tytxButton);
        sjtButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                
                controller.getJmeCanvas().useForFXOper(AllGlobal.SHEJITU, 30);
            }
        });
        tytsButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                if(dclist.size()>0){
                    DiCeng_Model dcm=dclist.get(pagination.getCurrentPageIndex());
                    System.out.println(pagination.getCurrentPageIndex());
                    float jl=Float.parseFloat(dcm.getJl());
                    controller.getJmeCanvas().useForFXOper(AllGlobal.TOUYINGTU, jl+1);
                }
            }
        });
        tytxButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent t) {
                if(dclist.size()>0){
                    DiCeng_Model dcm=dclist.get(pagination.getCurrentPageIndex());
                    System.out.println(pagination.getCurrentPageIndex());
                    float jl=Float.parseFloat(dcm.getJl());
                    float hdF=Float.parseFloat(dcm.getHd());
                    controller.getJmeCanvas().useForFXOper(AllGlobal.TOUYINGTU, jl+1+hdF);
                }
            }
        });
        fxContainer.setScene(scene);
    }
    public Group createPage(int pageIndex) {      
        
        DiCeng_Model dcm=new DiCeng_Model();
        if(dclist.size()>0){
            dcm=dclist.get(pageIndex);
        }
        Group page=new Group();
        GridPane gp=new GridPane();
        gp.setPrefSize(140, 200);
        //添加标签
        Label mcLabel=new Label("名称:");
        Text mcText=new Text(dcm.getMc());
        mcText.setFill(Color.RED);
        Label mcjLabel=new Label("煤层距:");
        Text mcjText=new Text(dcm.getJl()+"m");
        mcjText.setFill(Color.RED);
        Label hdLabel=new Label("岩层厚度:");
        Text hdText=new Text(dcm.getHd()+"m");
        hdText.setFill(Color.RED);
        Label sfLabel=new Label("该层上方有:");
        Text sfText=new Text((pageIndex+1)+"个岩层");
        sfText.setFill(Color.RED);
        Label xfLabel=new Label("该层下方有:");
        Text xfText=new Text((dclist.size()-pageIndex-1)+"个岩层");
        xfText.setFill(Color.RED);
        gp.add(mcLabel, 0, 0);
        gp.add(mcText, 1, 0);
        gp.add(mcjLabel, 0, 1);
        gp.add(mcjText, 1, 1);
        gp.add(hdLabel, 0, 2);
        gp.add(hdText, 1, 2);
        gp.add(sfLabel, 0, 3);
        gp.add(sfText, 1, 3);
        gp.add(xfLabel, 0, 4);
        gp.add(xfText, 1, 4);
        gp.setVgap(11);
        gp.setHgap(15);
        page.setTranslateX(5);
        page.setTranslateY(80);
        page.getChildren().add(gp);
        return page;
    }
    
    //添加控制器
    public void addController(MyController controller){
        this.controller=controller;
    }
    public void addBrain(MyBrain brain){
        this.brain=brain;
    }
    public void setPage(){
        dclist = OperateMyDatabaseImpl.doGetDiCengAll();
        pagination.setPageCount(dclist.size());
        pagination.setPageFactory(new Callback<Integer, Node>() {
            @Override
            public Node call(Integer pageIndex) {          
                return createPage(pageIndex);               
            }
        });
    }
}
